package com.example.server.Service;

import com.example.server.entity.User;
import com.example.server.mapper.Usermapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.security.NoSuchAlgorithmException;
import java.util.List;

import static com.example.server.Outto.Hashwith.hashWithSHA256;

@Service
public class UserService {
    @Autowired
    private Usermapper usera;


    //登录验证
    public User se(String username, String password) throws NoSuchAlgorithmException {
        String Password = hashWithSHA256(password);
        return usera.se(username, Password);
    }
    //注册插入
    public boolean re(User user) throws NoSuchAlgorithmException {
        User b = usera.sele(user.getEmail(),user.getUsername());
        if(b!=null)
            return false;
        else
        {
            String password = hashWithSHA256(user.getPassword());
            usera.re(user.getEmail(),password,user.getUsername());
            return true;
        }
    }



    public User self(String uid) {
        return usera.self(uid);
    }

    public boolean update(User ua) {
        String name = ua.getUsername();
        String email = ua.getEmail();
        String update = ua.getUpdated_at();
        int uid = ua.getUid();
        String sex = ua.getSex();
        String telephone = ua.getTelephone();
        System.out.println(name+email+update+uid+sex+telephone);
        User ab = usera.sele2(email,name);
        if(ab!=null)
            return false;
        else
       return usera.update(name,email,update,uid,sex,telephone);
    }

    public int select() {
        return usera.select();
    }

    public List<User> get() {
        return usera.getall();
    }

    public int deleteuser(int id) {
        return usera.deleteuser(id);
    }
}
